import axios from "axios";
import qs from "qs";

// const { CancelToken } = axios;
// window.cancelRequest = new Map();
const request = (url,opt) => {
    const companyCode = 'fulan';
    const siteCode = 'edu';
    let param = {
        headers:{
            'UPLOAD-TYPE': "ALI_OSS",
            Authorization: sessionStorage.getItem("token")
        }
    }
    if(opt.method == "get"){
        if(opt.data && Object.keys(opt.data).length){
            param.url = `${url}?companyCode=${companyCode}&siteCode=${siteCode}&${qs.stringify(opt.data)}`
        }else{
            param.url = `${url}?companyCode=${companyCode}&siteCode=${siteCode}`;
        }
    } else if(opt.method == "post"){
        param.url = `${url}?companyCode=${companyCode}&siteCode=${siteCode}`;
        param.method = "post";
        param.data = opt.data;
    }

    // param.cancelToken = new CancelToken(cancel => {
    //     window.cancelRequest.set(Symbol(Date.now()), {
    //       pathname: window.location.pathname,
    //       cancel,
    //     });
    //   });
    // axios.interceptors.request.use(function (config) {
    //     config.headers.Authorization = sessionStorage.getItem("token");
    //     config.headers['UPLOAD-TYPE'] = "ALI_OSS";
    //     config.timeout = 3000;
    //     return config;
    //   }, function (error) {
    //     // 对请求错误做些什么
    //     return Promise.reject(error);
    //   });

    return axios(param).then(res=>{
        //   console.log('xxx', res)
          if(res.status == 200){
              return res.data
          }else{
              console.log("request--response-status",res.status)
          }
    }).catch(error => {
        console.log('request--error', error)
    })
}

export default request;